const state = {
  visitedTabs: [],
  currentTab: null
}
// title: '工作台',
// name: '1',
// class: 'leftmenu-desktop',
// activeScene: 'defaultIndex',
// sceneType: 0

const mutations = {
  ADD_VISITED_TAB: (state, view) => {
    if (state.visitedTabs.some(v => v.name === view.name)) {
      state.currentTab = view
      return
    }
    state.visitedTabs.push(view)
    state.currentTab = view
  },
  DEL_VISITED_TAB: (state, view) => {
    for (const [i, v] of state.visitedTabs.entries()) {
      if (v.name === view.name) {
        state.visitedTabs.splice(i, 1)
        break
      }
    }
    state.currentTab = state.visitedTabs.length > 0 ? state.visitedTabs[0] : null
  }
}

const actions = {
  addVisitedTab({
    commit
  }, view) {
    commit('ADD_VISITED_TAB', view)
  },
  delVisitedTab({
    commit,
    state
  }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_TAB', view)
      resolve([...state.visitedTabs])
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
